﻿using System;
using System.Text;
using System.Collections.Generic;
using Xunit;

namespace TestXunit
{
    public sealed class TestEqual
    {
        [Fact]
        public void SimpleDemo()
        {
            Assert.Equal(9, 3 + 6);

            var sb = new StringBuilder().Append("hello ").Append("stasi");
            Assert.Equal("hello stasi", sb.ToString());
        }

        [Fact]
        public void TestWithBuiltinComparer()
        {
            string expected = "STASI";
            string actual = "stasi";

            Assert.False(expected == actual);
            Assert.NotEqual(expected, actual);
            Assert.Equal(expected, actual, StringComparer.OrdinalIgnoreCase);
        }

        private sealed class Point
        {
            public int X { get; private set; }
            public int Y { get; private set; }

            public Point(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        private sealed class PointComparer : IEqualityComparer<Point>
        {
            public bool Equals(Point p1, Point p2)
            {
                return (p1.X == p2.X) && (p1.Y == p2.Y);
            }

            public int GetHashCode(Point p)
            {
                return string.Format("{0},{1}", p.X, p.Y).GetHashCode();
            }
        }

        [Fact]
        public void TestCustomComparer()
        {
            var p1 = new Point(6, 9);
            var p2 = new Point(6, 9);

            Assert.NotEqual(p1, p2);
            Assert.Equal(p1, p2, new PointComparer());
        }
    }
}
